package com.resou.reader.data;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.ActivityRechargePrice;
import com.resou.reader.api.entry.FriendHelp;
import com.resou.reader.api.entry.IsFirstOpen;
import com.resou.reader.api.entry.LoginData;
import com.resou.reader.api.entry.ParticipationCheck;
import com.resou.reader.api.entry.RechargeActivityRule;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.entry.ResultList;
import com.resou.reader.api.entry.UserInfoBean;
import com.resou.reader.api.service.ActivityService;
import com.resou.reader.api.service.UserService;
import com.resou.reader.data.signin.SignInService;
import com.resou.reader.data.signin.model.SignIn;
import com.resou.reader.data.signin.model.SignInInfo2;
import com.resou.reader.mine.IView.UserInstance;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;
import retrofit2.Call;
import retrofit2.Response;

public class ResouxsRepository {
    private static ResouxsRepository sInstance;
    private final ActivityService mActivityService;
    private final SignInService mSignInService;
    private final UserService mUserService;

    private ResouxsRepository() {
        mActivityService = Injection.provideActivityService();
        mSignInService = Injection.provideSignInService();
        mUserService = Injection.provideUserService();
    }

    public static ResouxsRepository getInstance() {
        if (sInstance == null) {
            sInstance = new ResouxsRepository();
        }
        return sInstance;
    }

    public Observable<ResultList<FriendHelp>> loadFriendHelpList(String token) {
        return mActivityService.getFriendHelpList(token);
    }

    public Observable<Result<SignInInfo2>> loadSignInfo2(String token) {
        return mSignInService.getSignInInfo2(token);
    }

    public Observable<Result<SignIn>> signIn(@Nullable String token, int weekDay) {
        return mSignInService.signIn(token, weekDay);
    }

    public Observable<Result> receiveWelfare(@Nullable String token, @Nullable String taskId) {
        return mSignInService.receiveWelfare(token, taskId);
    }

    public Observable<Result<RechargeActivityRule>> loadRechargeActivityInfo(String token) {
        return mActivityService.getRechargeActivityInfo(token);
    }

    /**
     * <p>
     * 是否参加过十五日活动
     *
     * @param token
     * @return 1：是;0：否
     */
    public Boolean checkIsInActivity(String token) {
        Call<Result<ParticipationCheck>> resultCall = mActivityService.checkIsInActivity(token);
        try {
            Response<Result<ParticipationCheck>> response = resultCall.execute();
            if (response.isSuccessful()) {
                Result<ParticipationCheck> body = response.body();
                if (body != null && body.getData().getIsParticipation() == 1) {
                    //参加过活动
                    return true;
                } else {
                    //未参加
                    return false;
                }
            } else {
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 校验是否应该打开十五日活动弹窗
     */
    public Boolean checkIsShouldOpenDialog(String token, String clientType) {
        Call<Result<IsFirstOpen>> resultCall = mActivityService.checkIsFirstOpenAppInDay(token, clientType);
        try {
            Response<Result<IsFirstOpen>> response = resultCall.execute();
            if (response.isSuccessful()) {
                if (response.body() != null && response.body().getData().getIsFisrstOpen() == 1) {
                    return true;
                }
            } else {
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void setFirstOpenState(String token, String clientType) {
        try {
            mActivityService.setFirstOpen(token, clientType).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Boolean checkRechargeActivity(String token) {
        try {
            Response<Result<Integer>> response = mActivityService.checkIsJoinRechargeActivity(token).execute();
            if (response.isSuccessful()) {
                if (response.body().getData() == 1) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    public Observable<ResultList<ActivityRechargePrice>> getRechargePrice(int activityId) {
        return mActivityService.getRechargePrice(activityId);
    }

    public Observable<Result> checkUserIsPayInRechargeActivity(String token) {
        return mActivityService.checkUserIsPayInRechargeActivity(token);
    }

    public Observable<LoginData> loadUserInfo(String token) {
        return mUserService.getUserInfo(token)
                .flatMap((Function<Result<UserInfoBean>, Observable<LoginData>>) userInfoBeanResult -> {
                    UserInfoBean data = userInfoBeanResult.getData();
                    return Observable.just(UserInstance.saveUserInfo(data.getU()));
                });
    }
}
